﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;

namespace Trig_AMJ_IT_Customforms.Layouts.Trig_AMJ_IT_Forms
{
    public partial class E_Mail_ID_Request_Print : LayoutsPageBase
    {
        SPHelper obj = new SPHelper();

        #region "Properties"
        string CurrentUser = SPContext.Current.Web.CurrentUser.LoginName;
        string ErrorMsgs;

        public static string SiteURL { get; set; }
        public static string EmailReqNo { get; set; }
        public static string MainLstName { get; set; }
        public static string TaskLstName { get; set; }
        public static string CmntsLstName { get; set; }
        public static string lstGuid { get; set; }
        public string camlQry { get; set; }

        public static string AttLibName;
        public static string WorkflowConfigList;
        public static string Mode { get; set; }
        public static string RefNo;


        string WFCurrentOrder = string.Empty;
        string WFTaskType = string.Empty;
        public static string DocumentTypesList { get; set; }

        public static string ReqEmpDivision;
        public static string ReqEmpDept;

        Demand_Voucher_FormClass demandVoucherclass = new Demand_Voucher_FormClass(); // Helper class for this solution
        Trig_WF trigWFClass = new Trig_WF();

        string[] buttonVisibilty = new string[5];

        Label EmpName; Label EmpID; Label Division; Label Designation; Label MobNo;

        #endregion

        public E_Mail_ID_Request_Print()
        {
            SiteURL = SPContext.Current.Site.Url;
            SiteURL = SiteURL + "/sites/forms/";
            MainLstName = "EMail ID Request Forms";
            TaskLstName = "EMail ID Request Tasks";
            CmntsLstName = "EMail ID Request Comments";
            AttLibName = "Attachments";
            WorkflowConfigList = "Workflow Configuration List";
            DocumentTypesList = "DocumentTypes";
        }

        #region "User control manipulation"
        /// <summary>
        /// Get user control controls using find control method 
        /// </summary>
        private void GetUserControls()
        {

            EmpName = userInfo.FindControl("lblEmpName") as Label;
            EmpID = userInfo.FindControl("lblEmpID") as Label;
            Division = userInfo.FindControl("lblDivision") as Label;
            Designation = userInfo.FindControl("lblDesignation") as Label;
            MobNo = userInfo.FindControl("lblMobNo") as Label;
        }

        #endregion


        protected void Page_Load(object sender, EventArgs e)
        {

            Mode = Request.QueryString["Mode"];

            GetUserControls();

            if (!IsPostBack)
            {
                RefNo = Convert.ToString(Request.Params["RefID"]);

                if (Mode == "Print" || Mode == "View")
                {
                    ReLoadDetails();
                }
                else
                {
                    pnlFullForm.Visible = false;
                    pnlAccessDenied.Visible = true;
                    lblAccessDeniedMsg.Visible = true;
                    lblAccessDeniedMsg.Text = "Unable to open the form. Value is missing. Contact administrator";
                }
            }

        }

        /// <summary>
        /// Function to bind data after the Form gets submitted.
        /// </summary>
        /// <param name="SiteURL"></param>

        #region "Reload Forms"

        public void ReLoadDetails()
        {
            try
            {
                camlQry = "<Where><Eq><FieldRef Name=\"Reference_x0020_Number\" /><Value Type=\"Text\">" + RefNo + "</Value></Eq></Where>";
                SPListItem lstDatas = obj.GetSPListItem(SiteURL, MainLstName, camlQry);
                if (lstDatas != null)
                {
                    lblEmailReqNo.Text = Convert.ToString(lstDatas["Reference_x0020_Number"]);
                    dtpDate.Text = Convert.ToDateTime(lstDatas["RequestDate"]).ToString("dd/MM/yyyy");

                    pnlMailCreateInfo.Visible = true;

                    if (Request.QueryString["Mode"] == "View" || Request.QueryString["Mode"] == "Print")
                    {
                        txtUserName.Text = Convert.ToString(lstDatas["UserName"]);
                        if (string.IsNullOrEmpty(txtUserName.Text))
                            pnlMailCreateInfo.Visible = false;
                        txtemailAdd.Text = Convert.ToString(lstDatas["EmailAddress"]);
                        txtViewPass.Text = Convert.ToString(lstDatas["Password"]);

                    }


                    txtFirstName.Text = Convert.ToString(lstDatas["FirstName"]);
                    txtLastName.Text = Convert.ToString(lstDatas["LastName"]);

                    lblDivision.Text = (Convert.ToString(lstDatas["Division"]));

                    lblDepartment.Text = Convert.ToString(lstDatas["Department"]);
                    txtMailUserMobNo.Text = Convert.ToString(lstDatas["MobileNo"]);
                    txtUserNameSuggestion.Text = Convert.ToString(lstDatas["UserNameSuggestion"]);

                    if (Request.QueryString["Mode"] == "Edit")
                    {
                        txtUserName.Text = Convert.ToString(lstDatas["UserName"]);
                        txtemailAdd.Text = Convert.ToString(lstDatas["EmailAddress"]);
                    }

                    Label lblFormID = userInfo.FindControl("lblFormID") as Label;
                    lblFormID.Text = lstDatas.ParentList.ID.ToString();

                    obj.GetApprovedUsers(RefNo, SiteURL, TaskLstName, gvApprovers);

                }
            }
            catch (Exception ex)
            {
                ErrorMsgs = "Trig_AMJ_IT_Forms :: E-Mail ID Request" + " :: " + "Parent" + " :: " + "ReLoadDetails";
                obj.GenerateExceptionLog(ErrorMsgs, ex.Message, "");
            }
        }

        #endregion


    }
}
